<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacher.name" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number
          v-model="teacher.sort"
          controls-position="right"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
          -->
          <el-option :value="1" label="高级讲师" />
          <el-option :value="2" label="首席讲师" />
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" />
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro" :rows="10" type="textarea" />
      </el-form-item>
        <!-- 
        action="#" 后台的地址
        :auto-upload="false" 是否自动上传，不需要，我们要点击保存的时候上传
        :on-change="handleChange" 每次修改头像照片需要调用的方法
      -->
      <!-- 讲师头像：TODO -->
      <el-form-item label="头像上传">
        <el-upload
          class="avatar-uploader"
          action="#"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="handleChange"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacherApi from "@/api/edu/teacher";
import axios from "axios";

export default {
  data() {
    return {
      teacher: {},
      saveBtnDisabled: false,
      imageUrl:"",
      file:""
    };
  },
  created() {
    // 组件重用，不重复加载
     if (this.$route.params && this.$route.params.id) {
      this.getTeacherById(this.$route.params.id);
    } else {
      this.teacher = {};
    }
  },
  
  methods: {
   
    saveOrUpdate() {
      this.saveBtnDisabled = true;
      if (this.teacher.id) {
        this.updateTeacherById();
      } else {
        this.addTeacher();
      }
    },
    addTeacher() {
      let fd=new FormData();
      fd.append("file",this.file);
      teacherApi.upload(fd).then(response=>{
        this.teacher.avatar=response.data.url;
        teacherApi.addTeacher(this.teacher).then(response => {
        // 成功之后跳转页面，列表显示 - 路由的跳转
        this.$router.push({ path: "/teacher/list" });
      })
      
      });
    },
    updateTeacherById() {
      let fd=new FormData();
      fd.append("file",this.file);
      teacherApi.upload(fd).then(response=>{
        this.teacher.avatar=response.data.url;
        teacherApi.updateTeacherById(this.teacher).then(response => {
        this.$router.push({ path: "/teacher/list" });
      })
      
      });
    },
    getTeacherById(id) {
      teacherApi.getTeacherById(id).then(response => {
        this.teacher = response.data.item;
        this.imageUrl=response.data.item.avatar;
      });
    },
    handleChange(file){
      this.imageUrl=URL.createObjectURL(file.raw);
      this.file=file.raw;
    },
     
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
